import {TABLE_NAME} from "../../service/unit/mysql/config";
import DBConnection from "../../service/unit/mysql/DBConnection";
import File from "../../service/file/File";

interface IArticleTitleData {
    [index:string]:any
}

/**
 * 在数据库与硬盘中更新文件名
 * */
export default async function updateArticleTitle(conn:DBConnection, data:IArticleTitleData, condition:IArticleTitleData) {
    const {storeId} = condition;
    const {title} = data;
    const queryResult = await conn.Query.queryWhereNoneConditionEncode(
        TABLE_NAME.TABLE_NAME_ARTICLE,{id:storeId});
    const _PATH = queryResult[0].path;
    const _queryResultTitle = queryResult[0].title;
    const oldPath = File.pathSplice(_PATH, _queryResultTitle);
    const newPath = File.pathSplice(_PATH, title);

    await File.rename(oldPath, newPath);
    return await conn.Update.updateNoneEncodeValue(
        TABLE_NAME.TABLE_NAME_ARTICLE,
        {title},
        {KEY:'id',SIGN:'=',VALUE:storeId}).
    then(()=>{return 'ok'})

}